﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration; 
using System.Data.SqlClient; 

namespace LOGIN
{
    public partial class Input : Form
    {
        private DataTable InputTable;
        private DataView InputViewByYear;
        private DataView InputViewByMedicine;
        public Input()
        {
            InitializeComponent();
            this.dgv_InputTable.AllowUserToAddRows = false;
            this.dgv_InputTable.BackgroundColor = Color.White;
            this.dgv_InputTable.AutoSizeColumnsMode =
                DataGridViewAutoSizeColumnsMode.AllCells; 
        }

        private void Input_Load(object sender, EventArgs e)
        {
            time.Text = DateTime.Now.ToLongDateString().ToString();
            name.Text = ordersHelper.UserName;
           
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString =
                "Server=(local);Database=MyHospital;Integrated Security=sspi";
            SqlCommand sqlCommand = new SqlCommand();
            SqlCommand sqlCommand1 = new SqlCommand();
           
            sqlCommand.Connection = sqlConnection;
            sqlCommand1.Connection = sqlConnection;

            sqlCommand.CommandText = "select OrderID,SupplyID,OrderKinds,OrderDate,GetDate,ProducePla,State,c.DrugName,cs.IndPrice,Num,TolPrice,cs.Guige FROM orders cs JOIN dbo.medicineData c ON cs.MedicineID=c.MedicineID WHERE State='0';";
            sqlCommand1.CommandText = "select SupplyID,Supplyname from supplyer;";
            
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
            sqlDataAdapter.SelectCommand = sqlCommand;
            DataTable ordersTable = new DataTable();

            SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter();                                         
            sqlDataAdapter1.SelectCommand = sqlCommand1;
            DataTable supplyerTable = new DataTable();  
                                  


            sqlDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;                           
            this.InputTable = new DataTable();                 

            sqlConnection.Open();

            sqlDataAdapter.Fill(this.InputTable);
        
            sqlDataAdapter1.Fill(supplyerTable);

            sqlConnection.Close();
            this.InputTable.TableName = "tablename";
            this.InputViewByMedicine = new DataView();
            this.InputViewByMedicine.Table = this.InputTable;
            this.InputViewByMedicine.Sort = "DrugName ASC";

            this.InputViewByYear = new DataView();
            this.InputViewByYear.Table = this.InputTable;
            this.InputViewByYear.Sort = "OrderDate ASC";

            

            this.dgv_InputTable.DataSource = InputTable;
            dgv_InputTable.Columns["OrderID"].ReadOnly = true;
            dgv_InputTable.Columns["OrderKinds"].ReadOnly = true;
            dgv_InputTable.Columns["OrderDate"].ReadOnly = true;
            dgv_InputTable.Columns["GetDate"].ReadOnly = true;
            dgv_InputTable.Columns["ProducePla"].ReadOnly = true;
            dgv_InputTable.Columns["DrugName"].ReadOnly = true;
            dgv_InputTable.Columns["IndPrice"].ReadOnly = true;
            dgv_InputTable.Columns["Num"].ReadOnly = true;
            dgv_InputTable.Columns["TolPrice"].ReadOnly = true;
            dgv_InputTable.Columns["Guige"].ReadOnly = true;
            dgv_InputTable.Columns["OrderID"].HeaderText = "订单号";
            dgv_InputTable.Columns["SupplyID"].Visible = false;
            dgv_InputTable.Columns["OrderKinds"].HeaderText = "订单";
            dgv_InputTable.Columns["OrderDate"].HeaderText = "订单日期";
            dgv_InputTable.Columns["GetDate"].HeaderText = "到货日期";
            dgv_InputTable.Columns["ProducePla"].HeaderText = "生产地";
            dgv_InputTable.Columns["State"].HeaderText = "是否入库";
            dgv_InputTable.Columns["DrugName"].HeaderText = "药品";
            dgv_InputTable.Columns["IndPrice"].HeaderText = "单价";
            dgv_InputTable.Columns["Num"].HeaderText = "数量";
            dgv_InputTable.Columns["TolPrice"].HeaderText = "总价";
            dgv_InputTable.Columns["Guige"].HeaderText = "规格";
            this.dgv_InputTable.Columns[this.dgv_InputTable.Columns.Count - 1].AutoSizeMode =                         
                DataGridViewAutoSizeColumnMode.Fill;

            DataGridViewComboBoxColumn supplyerColumn = new DataGridViewComboBoxColumn();                      
            supplyerColumn.Name = "Supplyname";                                                                     
            supplyerColumn.HeaderText = "供应商";                                                             
            supplyerColumn.DataSource = supplyerTable;
            supplyerColumn.DisplayMember = "Supplyname";                                                             
            supplyerColumn.ValueMember = "SupplyID";                                                                 
            supplyerColumn.DataPropertyName = "SupplyID";                                                 
            supplyerColumn.DisplayIndex = 1;                                                                   
                          
            this.dgv_InputTable.Columns.Add(supplyerColumn);

            DataGridViewColumn medicineColumn = new DataGridViewColumn();                   
            medicineColumn.Name = "medicine";                                                                    
            medicineColumn.HeaderText = "药品名";
            label7.Text = ordersHelper.RoleID;
            
        }

        private void ruku_Click(object sender, EventArgs e)
        {
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString =
                ConfigurationManager.ConnectionStrings["Sql"].ConnectionString;
            SqlCommand updateCommand = new SqlCommand();                                                    
            updateCommand.Connection = sqlConnection;
            updateCommand.CommandText =                                                                        
                "UPDATE orders"
                + " SET state=@state"
                + " WHERE OrderID=@OrderID;";
            updateCommand.Parameters.Add("@OrderID", SqlDbType.Char, 10, "OrderID");
            updateCommand.Parameters.Add("@state", SqlDbType.Int, 0, "state");
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                           
            sqlDataAdapter.UpdateCommand = updateCommand;                                                   
            DataTable studentTable1 = (DataTable)this.dgv_InputTable.DataSource;                                
            sqlConnection.Open();                                                                      
            int rowAffected = sqlDataAdapter.Update(studentTable1);                                         
            sqlConnection.Close();                                                                          
            MessageBox.Show("更新" + rowAffected.ToString() + "行。");

           
            
        }

        private void dgv_InputTable_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgv_InputTable.SelectedCells.Count != 0)
            {
                int index = dgv_InputTable.CurrentRow.Index; 
                this.order.Text = dgv_InputTable.Rows[index].Cells[2].Value.ToString();
            }
        }

        private void search_Click(object sender, EventArgs e)
        {
            if (this.txb_search.Text == "")
            {
                MessageBox.Show("请输入搜索内容");
            }
            else if (this.comb_search.SelectedIndex == 0 && this.txb_search.Text != "")
            {
                DataRow searchResultRow = this.InputTable.Rows.Find(this.txb_search.Text.Trim());
                DataTable searchResultTable = this.InputTable.Clone();
                searchResultTable.ImportRow(searchResultRow);
                this.dgv_InputTable.DataSource = searchResultTable;
            }
            else if (this.comb_search.SelectedIndex == 1 && this.txb_search.Text != "")
            {
                DataRowView[] searchResultRowViews =
                this.InputViewByMedicine.FindRows(this.txb_search.Text.Trim());
                DataTable searchResultTable = this.InputTable.Clone();
                foreach (DataRowView dataRowView1 in searchResultRowViews)
                {
                    searchResultTable.ImportRow(dataRowView1.Row);
                }
                this.dgv_InputTable.DataSource = searchResultTable;
            }
            else if (this.comb_search.SelectedIndex == 2 && this.txb_search.Text != "")
            {
                
                DataRowView[] searchResultRowViews =
                this.InputViewByYear.FindRows( this.txb_search.Text.Trim());
                DataTable searchResultTable = this.InputTable.Clone();
                foreach (DataRowView dataRowView1 in searchResultRowViews)
                {
                    searchResultTable.ImportRow(dataRowView1.Row);
                }
                this.dgv_InputTable.DataSource = searchResultTable;
            }
        }

        
       


        
       

     

      
      

    }
}
